home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.5 / lib-tk / turtle.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2007-05-11  |  28.8 KB  |  1,107 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''
  5. Turtle graphics is a popular way for introducing programming to
  6. kids. It was part of the original Logo programming language developed
  7. by Wally Feurzeig and Seymour Papert in 1966.
  8.  
  9. Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it
  10. the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
  11. the direction it is facing, drawing a line as it moves. Give it the
  12. command turtle.left(25), and it rotates in-place 25 degrees clockwise.
  13.  
  14. By combining together these and similar commands, intricate shapes and
  15. pictures can easily be drawn.
  16. '''
  17. from math import *
  18. import Tkinter
  19. speeds = [
  20.     'fastest',
  21.     'fast',
  22.     'normal',
  23.     'slow',
  24.     'slowest']
  25.  
  26. class Error(Exception):
  27.     pass
  28.  
  29.  
  30. class RawPen:
  31.     
  32.     def __init__(self, canvas):
  33.         self._canvas = canvas
  34.         self._items = []
  35.         self._tracing = 1
  36.         self._arrow = 0
  37.         self._delay = 10
  38.         self._angle = 0
  39.         self.degrees()
  40.         self.reset()
  41.  
  42.     
  43.     def degrees(self, fullcircle = 360):
  44.         ''' Set angle measurement units to degrees.
  45.  
  46.         Example:
  47.         >>> turtle.degrees()
  48.         '''
  49.         if self._angle:
  50.             self._angle = (self._angle / self._fullcircle) * fullcircle
  51.         
  52.         self._fullcircle = fullcircle
  53.         self._invradian = pi / (fullcircle * 0.5)
  54.  
  55.     
  56.     def radians(self):
  57.         ''' Set the angle measurement units to radians.
  58.  
  59.         Example:
  60.         >>> turtle.radians()
  61.         '''
  62.         self.degrees(2 * pi)
  63.  
  64.     
  65.     def reset(self):
  66.         ''' Clear the screen, re-center the pen, and set variables to
  67.         the default values.
  68.  
  69.         Example:
  70.         >>> turtle.position()
  71.         [0.0, -22.0]
  72.         >>> turtle.heading()
  73.         100.0
  74.         >>> turtle.reset()
  75.         >>> turtle.position()
  76.         [0.0, 0.0]
  77.         >>> turtle.heading()
  78.         0.0
  79.         '''
  80.         canvas = self._canvas
  81.         self._canvas.update()
  82.         width = canvas.winfo_width()
  83.         height = canvas.winfo_height()
  84.         if width <= 1:
  85.             width = canvas['width']
  86.         
  87.         if height <= 1:
  88.             height = canvas['height']
  89.         
  90.         self._origin = (float(width) / 2, float(height) / 2)
  91.         self._position = self._origin
  92.         self._angle = 0
  93.         self._drawing = 1
  94.         self._width = 1
  95.         self._color = 'black'
  96.         self._filling = 0
  97.         self._path = []
  98.         self.clear()
  99.         canvas._root().tkraise()
  100.  
  101.     
  102.     def clear(self):
  103.         ''' Clear the screen. The turtle does not move.
  104.  
  105.         Example:
  106.         >>> turtle.clear()
  107.         '''
  108.         self.fill(0)
  109.         canvas = self._canvas
  110.         items = self._items
  111.         self._items = []
  112.         for item in items:
  113.             canvas.delete(item)
  114.         
  115.         self._delete_turtle()
  116.         self._draw_turtle()
  117.  
  118.     
  119.     def tracer(self, flag):
  120.         ''' Set tracing on if flag is True, and off if it is False.
  121.         Tracing means line are drawn more slowly, with an
  122.         animation of an arrow along the line.
  123.  
  124.         Example:
  125.         >>> turtle.tracer(False)   # turns off Tracer
  126.         '''
  127.         self._tracing = flag
  128.         if not self._tracing:
  129.             self._delete_turtle()
  130.         
  131.         self._draw_turtle()
  132.  
  133.     
  134.     def forward(self, distance):
  135.         ''' Go forward distance steps.
  136.  
  137.         Example:
  138.         >>> turtle.position()
  139.         [0.0, 0.0]
  140.         >>> turtle.forward(25)
  141.         >>> turtle.position()
  142.         [25.0, 0.0]
  143.         >>> turtle.forward(-75)
  144.         >>> turtle.position()
  145.         [-50.0, 0.0]
  146.         '''
  147.         (x0, y0) = start = self._position
  148.         x1 = x0 + distance * cos(self._angle * self._invradian)
  149.         y1 = y0 - distance * sin(self._angle * self._invradian)
  150.         self._goto(x1, y1)
  151.  
  152.     
  153.     def backward(self, distance):
  154.         """ Go backwards distance steps.
  155.  
  156.         The turtle's heading does not change.
  157.  
  158.         Example:
  159.         >>> turtle.position()
  160.         [0.0, 0.0]
  161.         >>> turtle.backward(30)
  162.         >>> turtle.position()
  163.         [-30.0, 0.0]
  164.         """
  165.         self.forward(-distance)
  166.  
  167.     
  168.     def left(self, angle):
  169.         ''' Turn left angle units (units are by default degrees,
  170.         but can be set via the degrees() and radians() functions.)
  171.  
  172.         When viewed from above, the turning happens in-place around
  173.         its front tip.
  174.  
  175.         Example:
  176.         >>> turtle.heading()
  177.         22
  178.         >>> turtle.left(45)
  179.         >>> turtle.heading()
  180.         67.0
  181.         '''
  182.         self._angle = (self._angle + angle) % self._fullcircle
  183.         self._draw_turtle()
  184.  
  185.     
  186.     def right(self, angle):
  187.         ''' Turn right angle units (units are by default degrees,
  188.         but can be set via the degrees() and radians() functions.)
  189.  
  190.         When viewed from above, the turning happens in-place around
  191.         its front tip.
  192.  
  193.         Example:
  194.         >>> turtle.heading()
  195.         22
  196.         >>> turtle.right(45)
  197.         >>> turtle.heading()
  198.         337.0
  199.         '''
  200.         self.left(-angle)
  201.  
  202.     
  203.     def up(self):
  204.         ''' Pull the pen up -- no drawing when moving.
  205.  
  206.         Example:
  207.         >>> turtle.up()
  208.         '''
  209.         self._drawing = 0
  210.  
  211.     
  212.     def down(self):
  213.         ''' Put the pen down -- draw when moving.
  214.  
  215.         Example:
  216.         >>> turtle.down()
  217.         '''
  218.         self._drawing = 1
  219.  
  220.     
  221.     def width(self, width):
  222.         ''' Set the line to thickness to width.
  223.  
  224.         Example:
  225.         >>> turtle.width(10)
  226.         '''
  227.         self._width = float(width)
  228.  
  229.     
  230.     def color(self, *args):
  231.         ''' Set the pen color.
  232.  
  233.         Three input formats are allowed:
  234.  
  235.             color(s)
  236.             s is a Tk specification string, such as "red" or "yellow"
  237.  
  238.             color((r, g, b))
  239.             *a tuple* of r, g, and b, which represent, an RGB color,
  240.             and each of r, g, and b are in the range [0..1]
  241.  
  242.             color(r, g, b)
  243.             r, g, and b represent an RGB color, and each of r, g, and b
  244.             are in the range [0..1]
  245.  
  246.         Example:
  247.  
  248.         >>> turtle.color(\'brown\')
  249.         >>> tup = (0.2, 0.8, 0.55)
  250.         >>> turtle.color(tup)
  251.         >>> turtle.color(0, .5, 0)
  252.         '''
  253.         if not args:
  254.             raise Error, 'no color arguments'
  255.         
  256.         if len(args) == 1:
  257.             color = args[0]
  258.             if type(color) == type(''):
  259.                 
  260.                 try:
  261.                     id = self._canvas.create_line(0, 0, 0, 0, fill = color)
  262.                 except Tkinter.TclError:
  263.                     raise Error, 'bad color string: %r' % (color,)
  264.  
  265.                 self._set_color(color)
  266.                 return None
  267.             
  268.             
  269.             try:
  270.                 (r, g, b) = color
  271.             raise Error, 'bad color sequence: %r' % (color,)
  272.  
  273.         else:
  274.             
  275.             try:
  276.                 (r, g, b) = args
  277.             except:
  278.                 raise Error, 'bad color arguments: %r' % (args,)
  279.  
  280.         if r <= r:
  281.             pass
  282.         elif not r <= 1:
  283.             raise AssertionError
  284.         if g <= g:
  285.             pass
  286.         elif not g <= 1:
  287.             raise AssertionError
  288.         if b <= b:
  289.             pass
  290.         elif not b <= 1:
  291.             raise AssertionError
  292.         x = 255
  293.         y = 0.5
  294.         self._set_color('#%02x%02x%02x' % (int(r * x + y), int(g * x + y), int(b * x + y)))
  295.  
  296.     
  297.     def _set_color(self, color):
  298.         self._color = color
  299.         self._draw_turtle()
  300.  
  301.     
  302.     def write(self, text, move = False):
  303.         """ Write text at the current pen position.
  304.  
  305.         If move is true, the pen is moved to the bottom-right corner
  306.         of the text. By default, move is False.
  307.  
  308.         Example:
  309.         >>> turtle.write('The race is on!')
  310.         >>> turtle.write('Home = (0, 0)', True)
  311.         """
  312.         (x, y) = self._position
  313.         x = x - 1
  314.         item = self._canvas.create_text(x, y, text = str(text), anchor = 'sw', fill = self._color)
  315.         self._items.append(item)
  316.         if move:
  317.             (x0, y0, x1, y1) = self._canvas.bbox(item)
  318.             self._goto(x1, y1)
  319.         
  320.         self._draw_turtle()
  321.  
  322.     
  323.     def fill(self, flag):
  324.         ''' Call fill(1) before drawing the shape you
  325.          want to fill, and fill(0) when done.
  326.  
  327.         Example:
  328.         >>> turtle.fill(1)
  329.         >>> turtle.forward(100)
  330.         >>> turtle.left(90)
  331.         >>> turtle.forward(100)
  332.         >>> turtle.left(90)
  333.         >>> turtle.forward(100)
  334.         >>> turtle.left(90)
  335.         >>> turtle.forward(100)
  336.         >>> turtle.fill(0)
  337.         '''
  338.         if self._filling:
  339.             path = tuple(self._path)
  340.             smooth = self._filling < 0
  341.             if len(path) > 2:
  342.                 item = self._canvas._create('polygon', path, {
  343.                     'fill': self._color,
  344.                     'smooth': smooth })
  345.                 self._items.append(item)
  346.             
  347.         
  348.         self._path = []
  349.         self._filling = flag
  350.         if flag:
  351.             self._path.append(self._position)
  352.         
  353.  
  354.     
  355.     def begin_fill(self):
  356.         ''' Called just before drawing a shape to be filled.
  357.             Must eventually be followed by a corresponding end_fill() call.
  358.             Otherwise it will be ignored.
  359.  
  360.         Example:
  361.         >>> turtle.begin_fill()
  362.         >>> turtle.forward(100)
  363.         >>> turtle.left(90)
  364.         >>> turtle.forward(100)
  365.         >>> turtle.left(90)
  366.         >>> turtle.forward(100)
  367.         >>> turtle.left(90)
  368.         >>> turtle.forward(100)
  369.         >>> turtle.end_fill()
  370.         '''
  371.         self._path = [
  372.             self._position]
  373.         self._filling = 1
  374.  
  375.     
  376.     def end_fill(self):
  377.         ''' Called after drawing a shape to be filled.
  378.  
  379.         Example:
  380.         >>> turtle.begin_fill()
  381.         >>> turtle.forward(100)
  382.         >>> turtle.left(90)
  383.         >>> turtle.forward(100)
  384.         >>> turtle.left(90)
  385.         >>> turtle.forward(100)
  386.         >>> turtle.left(90)
  387.         >>> turtle.forward(100)
  388.         >>> turtle.end_fill()
  389.         '''
  390.         self.fill(0)
  391.  
  392.     
  393.     def circle(self, radius, extent = None):
  394.         ''' Draw a circle with given radius.
  395.         The center is radius units left of the turtle; extent
  396.         determines which part of the circle is drawn. If not given,
  397.         the entire circle is drawn.
  398.  
  399.         If extent is not a full circle, one endpoint of the arc is the
  400.         current pen position. The arc is drawn in a counter clockwise
  401.         direction if radius is positive, otherwise in a clockwise
  402.         direction. In the process, the direction of the turtle is
  403.         changed by the amount of the extent.
  404.  
  405.         >>> turtle.circle(50)
  406.         >>> turtle.circle(120, 180)  # half a circle
  407.         '''
  408.         if extent is None:
  409.             extent = self._fullcircle
  410.         
  411.         frac = abs(extent) / self._fullcircle
  412.         steps = 1 + int(min(11 + abs(radius) / 6, 59) * frac)
  413.         w = 1 * extent / steps
  414.         w2 = 0.5 * w
  415.         l = 2 * radius * sin(w2 * self._invradian)
  416.         if radius < 0:
  417.             l = -l
  418.             w = -w
  419.             w2 = -w2
  420.         
  421.         self.left(w2)
  422.         for i in range(steps):
  423.             self.forward(l)
  424.             self.left(w)
  425.         
  426.         self.right(w2)
  427.  
  428.     
  429.     def heading(self):
  430.         """ Return the turtle's current heading.
  431.  
  432.         Example:
  433.         >>> turtle.heading()
  434.         67.0
  435.         """
  436.         return self._angle
  437.  
  438.     
  439.     def setheading(self, angle):
  440.         ''' Set the turtle facing the given angle.
  441.  
  442.         Here are some common directions in degrees:
  443.  
  444.            0 - east
  445.           90 - north
  446.          180 - west
  447.          270 - south
  448.  
  449.         Example:
  450.         >>> turtle.setheading(90)
  451.         >>> turtle.heading()
  452.         90
  453.         >>> turtle.setheading(128)
  454.         >>> turtle.heading()
  455.         128
  456.         '''
  457.         self._angle = angle
  458.         self._draw_turtle()
  459.  
  460.     
  461.     def window_width(self):
  462.         ''' Returns the width of the turtle window.
  463.  
  464.         Example:
  465.         >>> turtle.window_width()
  466.         640
  467.         '''
  468.         width = self._canvas.winfo_width()
  469.         if width <= 1:
  470.             width = self._canvas['width']
  471.         
  472.         return width
  473.  
  474.     
  475.     def window_height(self):
  476.         ''' Return the height of the turtle window.
  477.  
  478.         Example:
  479.         >>> turtle.window_height()
  480.         768
  481.         '''
  482.         height = self._canvas.winfo_height()
  483.         if height <= 1:
  484.             height = self._canvas['height']
  485.         
  486.         return height
  487.  
  488.     
  489.     def position(self):
  490.         ''' Return the current (x, y) location of the turtle.
  491.  
  492.         Example:
  493.         >>> turtle.position()
  494.         [0.0, 240.0]
  495.         '''
  496.         (x0, y0) = self._origin
  497.         (x1, y1) = self._position
  498.         return [
  499.             x1 - x0,
  500.             -y1 + y0]
  501.  
  502.     
  503.     def setx(self, xpos):
  504.         """ Set the turtle's x coordinate to be xpos.
  505.  
  506.         Example:
  507.         >>> turtle.position()
  508.         [10.0, 240.0]
  509.         >>> turtle.setx(10)
  510.         >>> turtle.position()
  511.         [10.0, 240.0]
  512.         """
  513.         (x0, y0) = self._origin
  514.         (x1, y1) = self._position
  515.         self._goto(x0 + xpos, y1)
  516.  
  517.     
  518.     def sety(self, ypos):
  519.         """ Set the turtle's y coordinate to be ypos.
  520.  
  521.         Example:
  522.         >>> turtle.position()
  523.         [0.0, 0.0]
  524.         >>> turtle.sety(-22)
  525.         >>> turtle.position()
  526.         [0.0, -22.0]
  527.         """
  528.         (x0, y0) = self._origin
  529.         (x1, y1) = self._position
  530.         self._goto(x1, y0 - ypos)
  531.  
  532.     
  533.     def towards(self, *args):
  534.         '''Returs the angle, which corresponds to the line
  535.         from turtle-position to point (x,y).
  536.  
  537.         Argument can be two coordinates or one pair of coordinates
  538.         or a RawPen/Pen instance.
  539.  
  540.         Example:
  541.         >>> turtle.position()
  542.         [10.0, 10.0]
  543.         >>> turtle.towards(0,0)
  544.         225.0
  545.         '''
  546.         if len(args) == 2:
  547.             (x, y) = args
  548.         else:
  549.             arg = args[0]
  550.             if isinstance(arg, RawPen):
  551.                 (x, y) = arg.position()
  552.             else:
  553.                 (x, y) = arg
  554.         (x0, y0) = self.position()
  555.         dx = x - x0
  556.         dy = y - y0
  557.         return atan2(dy, dx) / self._invradian % self._fullcircle
  558.  
  559.     
  560.     def goto(self, *args):
  561.         """ Go to the given point.
  562.  
  563.         If the pen is down, then a line will be drawn. The turtle's
  564.         orientation does not change.
  565.  
  566.         Two input formats are accepted:
  567.  
  568.            goto(x, y)
  569.            go to point (x, y)
  570.  
  571.            goto((x, y))
  572.            go to point (x, y)
  573.  
  574.         Example:
  575.         >>> turtle.position()
  576.         [0.0, 0.0]
  577.         >>> turtle.goto(50, -45)
  578.         >>> turtle.position()
  579.         [50.0, -45.0]
  580.         """
  581.         if len(args) == 1:
  582.             
  583.             try:
  584.                 (x, y) = args[0]
  585.             raise Error, 'bad point argument: %r' % (args[0],)
  586.  
  587.         else:
  588.             
  589.             try:
  590.                 (x, y) = args
  591.             except:
  592.                 raise Error, 'bad coordinates: %r' % (args[0],)
  593.  
  594.         (x0, y0) = self._origin
  595.         self._goto(x0 + x, y0 - y)
  596.  
  597.     
  598.     def _goto(self, x1, y1):
  599.         (x0, y0) = self._position
  600.         self._position = map(float, (x1, y1))
  601.         if self._filling:
  602.             self._path.append(self._position)
  603.         
  604.         if self._drawing:
  605.             if self._tracing:
  606.                 dx = float(x1 - x0)
  607.                 dy = float(y1 - y0)
  608.                 distance = hypot(dx, dy)
  609.                 nhops = int(distance)
  610.                 item = self._canvas.create_line(x0, y0, x0, y0, width = self._width, capstyle = 'round', fill = self._color)
  611.                 
  612.                 try:
  613.                     for i in range(1, 1 + nhops):
  614.                         x = x0 + dx * i / nhops
  615.                         y = y0 + dy * i / nhops
  616.                         self._canvas.coords(item, x0, y0, x, y)
  617.                         self._draw_turtle((x, y))
  618.                         self._canvas.update()
  619.                         self._canvas.after(self._delay)
  620.                     
  621.                     self._canvas.coords(item, x0, y0, x1, y1)
  622.                     self._canvas.itemconfigure(item, arrow = 'none')
  623.                 except Tkinter.TclError:
  624.                     return None
  625.                 except:
  626.                     None<EXCEPTION MATCH>Tkinter.TclError
  627.                 
  628.  
  629.             None<EXCEPTION MATCH>Tkinter.TclError
  630.             item = self._canvas.create_line(x0, y0, x1, y1, width = self._width, capstyle = 'round', fill = self._color)
  631.             self._items.append(item)
  632.         
  633.         self._draw_turtle()
  634.  
  635.     
  636.     def speed(self, speed):
  637.         """ Set the turtle's speed.
  638.  
  639.         speed must one of these five strings:
  640.  
  641.             'fastest' is a 0 ms delay
  642.             'fast' is a 5 ms delay
  643.             'normal' is a 10 ms delay
  644.             'slow' is a 15 ms delay
  645.             'slowest' is a 20 ms delay
  646.  
  647.          Example:
  648.          >>> turtle.speed('slow')
  649.         """
  650.         
  651.         try:
  652.             speed = speed.strip().lower()
  653.             self._delay = speeds.index(speed) * 5
  654.         except:
  655.             raise ValueError('%r is not a valid speed. speed must be one of %s' % (speed, speeds))
  656.  
  657.  
  658.     
  659.     def delay(self, delay):
  660.         ''' Set the drawing delay in milliseconds.
  661.  
  662.         This is intended to allow finer control of the drawing speed
  663.         than the speed() method
  664.  
  665.         Example:
  666.         >>> turtle.delay(15)
  667.         '''
  668.         if int(delay) < 0:
  669.             raise ValueError('delay must be greater than or equal to 0')
  670.         
  671.         self._delay = int(delay)
  672.  
  673.     
  674.     def _draw_turtle(self, position = []):
  675.         if not self._tracing:
  676.             self._canvas.update()
  677.             return None
  678.         
  679.         if position == []:
  680.             position = self._position
  681.         
  682.         (x, y) = position
  683.         distance = 8
  684.         dx = distance * cos(self._angle * self._invradian)
  685.         dy = distance * sin(self._angle * self._invradian)
  686.         self._delete_turtle()
  687.         self._arrow = self._canvas.create_line(x - dx, y + dy, x, y, width = self._width, arrow = 'last', capstyle = 'round', fill = self._color)
  688.         self._canvas.update()
  689.  
  690.     
  691.     def _delete_turtle(self):
  692.         if self._arrow != 0:
  693.             self._canvas.delete(self._arrow)
  694.             self._arrow = 0
  695.         
  696.  
  697.  
  698. _root = None
  699. _canvas = None
  700. _pen = None
  701. _width = 0.5
  702. _height = 0.75
  703. _startx = None
  704. _starty = None
  705. _title = 'Turtle Graphics'
  706.  
  707. class Pen(RawPen):
  708.     
  709.     def __init__(self):
  710.         global _root, _canvas
  711.         if _root is None:
  712.             _root = Tkinter.Tk()
  713.             _root.wm_protocol('WM_DELETE_WINDOW', self._destroy)
  714.             _root.title(_title)
  715.         
  716.         if _canvas is None:
  717.             _canvas = Tkinter.Canvas(_root, background = 'white')
  718.             _canvas.pack(expand = 1, fill = 'both')
  719.             setup(width = _width, height = _height, startx = _startx, starty = _starty)
  720.         
  721.         RawPen.__init__(self, _canvas)
  722.  
  723.     
  724.     def _destroy(self):
  725.         global _pen, _root, _canvas
  726.         root = self._canvas._root()
  727.         if root is _root:
  728.             _pen = None
  729.             _root = None
  730.             _canvas = None
  731.         
  732.         root.destroy()
  733.  
  734.  
  735.  
  736. def _getpen():
  737.     global _pen
  738.     if not _pen:
  739.         _pen = Pen()
  740.     
  741.     return _pen
  742.  
  743.  
  744. class Turtle(Pen):
  745.     pass
  746.  
  747.  
  748. def degrees():
  749.     _getpen().degrees()
  750.  
  751.  
  752. def radians():
  753.     _getpen().radians()
  754.  
  755.  
  756. def reset():
  757.     _getpen().reset()
  758.  
  759.  
  760. def clear():
  761.     _getpen().clear()
  762.  
  763.  
  764. def tracer(flag):
  765.     _getpen().tracer(flag)
  766.  
  767.  
  768. def forward(distance):
  769.     _getpen().forward(distance)
  770.  
  771.  
  772. def backward(distance):
  773.     _getpen().backward(distance)
  774.  
  775.  
  776. def left(angle):
  777.     _getpen().left(angle)
  778.  
  779.  
  780. def right(angle):
  781.     _getpen().right(angle)
  782.  
  783.  
  784. def up():
  785.     _getpen().up()
  786.  
  787.  
  788. def down():
  789.     _getpen().down()
  790.  
  791.  
  792. def width(width):
  793.     _getpen().width(width)
  794.  
  795.  
  796. def color(*args):
  797.     _getpen().color(*args)
  798.  
  799.  
  800. def write(arg, move = 0):
  801.     _getpen().write(arg, move)
  802.  
  803.  
  804. def fill(flag):
  805.     _getpen().fill(flag)
  806.  
  807.  
  808. def begin_fill():
  809.     _getpen().begin_fill()
  810.  
  811.  
  812. def end_fill():
  813.     _getpen().end_fill()
  814.  
  815.  
  816. def circle(radius, extent = None):
  817.     _getpen().circle(radius, extent)
  818.  
  819.  
  820. def goto(*args):
  821.     _getpen().goto(*args)
  822.  
  823.  
  824. def heading():
  825.     return _getpen().heading()
  826.  
  827.  
  828. def setheading(angle):
  829.     _getpen().setheading(angle)
  830.  
  831.  
  832. def position():
  833.     return _getpen().position()
  834.  
  835.  
  836. def window_width():
  837.     return _getpen().window_width()
  838.  
  839.  
  840. def window_height():
  841.     return _getpen().window_height()
  842.  
  843.  
  844. def setx(xpos):
  845.     _getpen().setx(xpos)
  846.  
  847.  
  848. def sety(ypos):
  849.     _getpen().sety(ypos)
  850.  
  851.  
  852. def towards(*args):
  853.     return _getpen().towards(*args)
  854.  
  855.  
  856. def done():
  857.     _root.mainloop()
  858.  
  859.  
  860. def delay(delay):
  861.     return _getpen().delay(delay)
  862.  
  863.  
  864. def speed(speed):
  865.     return _getpen().speed(speed)
  866.  
  867. for methodname in dir(RawPen):
  868.     if not methodname.startswith('_'):
  869.         eval(methodname).__doc__ = RawPen.__dict__[methodname].__doc__
  870.     
  871.  
  872.  
  873. def setup(**geometry):
  874.     ''' Sets the size and position of the main window.
  875.  
  876.     Keywords are width, height, startx and starty:
  877.  
  878.     width: either a size in pixels or a fraction of the screen.
  879.       Default is 50% of screen.
  880.     height: either the height in pixels or a fraction of the screen.
  881.       Default is 75% of screen.
  882.  
  883.     Setting either width or height to None before drawing will force
  884.       use of default geometry as in older versions of turtle.py
  885.  
  886.     startx: starting position in pixels from the left edge of the screen.
  887.       Default is to center window. Setting startx to None is the default
  888.       and centers window horizontally on screen.
  889.  
  890.     starty: starting position in pixels from the top edge of the screen.
  891.       Default is to center window. Setting starty to None is the default
  892.       and centers window vertically on screen.
  893.  
  894.     Examples:
  895.     >>> setup (width=200, height=200, startx=0, starty=0)
  896.  
  897.     sets window to 200x200 pixels, in upper left of screen
  898.  
  899.     >>> setup(width=.75, height=0.5, startx=None, starty=None)
  900.  
  901.     sets window to 75% of screen by 50% of screen and centers
  902.  
  903.     >>> setup(width=None)
  904.  
  905.     forces use of default geometry as in older versions of turtle.py
  906.     '''
  907.     global _width, _height, _startx, _starty, _width, _height, _startx, _starty
  908.     width = geometry.get('width', _width)
  909.     if width >= 0 or width == None:
  910.         _width = width
  911.     else:
  912.         raise ValueError, 'width can not be less than 0'
  913.     height = geometry.get('height', _height)
  914.     if height >= 0 or height == None:
  915.         _height = height
  916.     else:
  917.         raise ValueError, 'height can not be less than 0'
  918.     startx = geometry.get('startx', _startx)
  919.     if startx >= 0 or startx == None:
  920.         _startx = _startx
  921.     else:
  922.         raise ValueError, 'startx can not be less than 0'
  923.     starty = geometry.get('starty', _starty)
  924.     if starty >= 0 or starty == None:
  925.         _starty = starty
  926.     else:
  927.         raise ValueError, 'startx can not be less than 0'
  928.     if _root and _width and _height:
  929.         if _width < _width:
  930.             pass
  931.         elif _width <= 1:
  932.             _width = _root.winfo_screenwidth() * +width
  933.         
  934.         if _height < _height:
  935.             pass
  936.         elif _height <= 1:
  937.             _height = _root.winfo_screenheight() * _height
  938.         
  939.         if _startx is None:
  940.             _startx = (_root.winfo_screenwidth() - _width) / 2
  941.         
  942.         if _starty is None:
  943.             _starty = (_root.winfo_screenheight() - _height) / 2
  944.         
  945.         _root.geometry('%dx%d+%d+%d' % (_width, _height, _startx, _starty))
  946.     
  947.  
  948.  
  949. def title(title):
  950.     '''Set the window title.
  951.  
  952.     By default this is set to \'Turtle Graphics\'
  953.  
  954.     Example:
  955.     >>> title("My Window")
  956.     '''
  957.     global _title
  958.     _title = title
  959.  
  960.  
  961. def demo():
  962.     reset()
  963.     tracer(1)
  964.     up()
  965.     backward(100)
  966.     down()
  967.     width(3)
  968.     for i in range(3):
  969.         if i == 2:
  970.             fill(1)
  971.         
  972.         for j in range(4):
  973.             forward(20)
  974.             left(90)
  975.         
  976.         if i == 2:
  977.             color('maroon')
  978.             fill(0)
  979.         
  980.         up()
  981.         forward(30)
  982.         down()
  983.     
  984.     width(1)
  985.     color('black')
  986.     tracer(0)
  987.     up()
  988.     right(90)
  989.     forward(100)
  990.     right(90)
  991.     forward(100)
  992.     right(180)
  993.     down()
  994.     write('startstart', 1)
  995.     write('start', 1)
  996.     color('red')
  997.     for i in range(5):
  998.         forward(20)
  999.         left(90)
  1000.         forward(20)
  1001.         right(90)
  1002.     
  1003.     fill(1)
  1004.     for i in range(5):
  1005.         forward(20)
  1006.         left(90)
  1007.         forward(20)
  1008.         right(90)
  1009.     
  1010.     fill(0)
  1011.     tracer(1)
  1012.     write('end')
  1013.  
  1014.  
  1015. def demo2():
  1016.     speed('fast')
  1017.     width(3)
  1018.     setheading(towards(0, 0))
  1019.     (x, y) = position()
  1020.     r = (x ** 2 + y ** 2) ** 0.5 / 2
  1021.     right(90)
  1022.     pendown = True
  1023.     for i in range(18):
  1024.         if pendown:
  1025.             up()
  1026.             pendown = False
  1027.         else:
  1028.             down()
  1029.             pendown = True
  1030.         circle(r, 10)
  1031.     
  1032.     sleep(2)
  1033.     reset()
  1034.     left(90)
  1035.     l = 10
  1036.     color('green')
  1037.     width(3)
  1038.     left(180)
  1039.     sp = 5
  1040.     for i in range(-2, 16):
  1041.         if i > 0:
  1042.             color(1 - 0.05 * i, 0, 0.05 * i)
  1043.             fill(1)
  1044.             color('green')
  1045.         
  1046.         for j in range(3):
  1047.             forward(l)
  1048.             left(120)
  1049.         
  1050.         l += 10
  1051.         left(15)
  1052.         if sp > 0:
  1053.             sp = sp - 1
  1054.             speed(speeds[sp])
  1055.             continue
  1056.     
  1057.     color(0.25, 0, 0.75)
  1058.     fill(0)
  1059.     left(120)
  1060.     up()
  1061.     forward(70)
  1062.     right(30)
  1063.     down()
  1064.     color('red')
  1065.     speed('fastest')
  1066.     fill(1)
  1067.     for i in range(4):
  1068.         circle(50, 90)
  1069.         right(90)
  1070.         forward(30)
  1071.         right(90)
  1072.     
  1073.     color('yellow')
  1074.     fill(0)
  1075.     left(90)
  1076.     up()
  1077.     forward(30)
  1078.     down()
  1079.     color('red')
  1080.     turtle = Turtle()
  1081.     turtle.reset()
  1082.     turtle.left(90)
  1083.     turtle.speed('normal')
  1084.     turtle.up()
  1085.     turtle.goto(280, 40)
  1086.     turtle.left(24)
  1087.     turtle.down()
  1088.     turtle.speed('fast')
  1089.     turtle.color('blue')
  1090.     turtle.width(2)
  1091.     speed('fastest')
  1092.     setheading(towards(turtle))
  1093.     while abs(position()[0] - turtle.position()[0]) > 4 or abs(position()[1] - turtle.position()[1]) > 4:
  1094.         turtle.forward(3.5)
  1095.         turtle.left(0.6)
  1096.         setheading(towards(turtle))
  1097.         forward(4)
  1098.     write('CAUGHT! ', move = True)
  1099.  
  1100. if __name__ == '__main__':
  1101.     from time import sleep
  1102.     demo()
  1103.     sleep(3)
  1104.     demo2()
  1105.     done()
  1106.  
  1107.